package com.chen.springboot.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.common.Result;
import com.chen.springboot.entity.Address;
import com.chen.springboot.mapper.AddressMapper;
import com.chen.springboot.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author Chen
 * @Date 2024/12/3 18:28
 */

@Api(tags = {"地址接口"})
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping
    @ApiOperation("查询所有地址")
    public Result<?> getPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam Integer userId) {
        Page<Address> page = addressService.getPage(pageNum, pageSize, userId);
        return Result.success(page);
    }

    /**
     * 新增地址
     * @param address
     * @return
     */

    @ApiOperation("新增地址")
    @PostMapping
    public Result<?> add(@RequestBody Address address) {
        addressService.add(address);
        return Result.success();
    }

    @ApiOperation("删除单个地址")
    @DeleteMapping("{id}")
    public Result<?> deleteByPath(@PathVariable Integer id) {
        addressService.deleteByPath(id);
        return Result.success();
    }

    @ApiOperation("删除所有地址")
    @DeleteMapping("/all")
    public Result<?> deleteAll() {
        addressService.deleteAll();
        return Result.success();
    }

    @ApiOperation("更新地址")
    @PutMapping
    public Result<?> update(@RequestBody Address address) {
        addressService.update(address);
        return Result.success();
    }

    @ApiOperation("更改地址状态")
    @GetMapping("/update")
    public Result<?> updateById(@RequestParam Integer id, @RequestParam Integer userId) {
        addressService.updateById(id, userId);
        return Result.success();
    }

    @ApiOperation("查询选择的地址")
    @GetMapping("/one")
    public Result<?> getOne(@RequestParam Integer userId) {
        String data=addressService.getOne(userId);
        return Result.success(data);
    }
}
